#include "testpress.h"

#ifdef _WIN32
#pragma comment(lib,"../../../lib/nspr/lib/libnspr4.lib")
#pragma comment(lib,"../../../lib/nspr/lib/libplc4.lib")
#pragma comment(lib,"../../../lib/nspr/lib/libplds4.lib")
#ifdef _DEBUG
#pragma comment(lib,"../../../lib/libapr/debug/libapr-1.lib")
#else
#pragma comment(lib,"../../../lib/libapr/release/libapr-1.lib")
#endif
#endif


static int kcc_base64_enc(lua_State *lua)
{
	char enc[KNET_IOBUFFER_SIZE];
	const char *data = kc_lua_get_string(lua, 1);
	
	memset(enc, 0, sizeof(enc));
	
	if (data == NULL) {
		kc_lua_push_nil(lua);
	}
	else {
		if (strlen(data) > KNET_IOBUFFER_SIZE / 2) {
			kc_lua_push_nil(lua);
		}
		else {
			kc_base64_encode(enc, data, strlen(data));
			kc_lua_push_string(lua, enc);
		}
	}
	
	return 1;
}

static int kcc_base64_dec(lua_State *lua)
{
	char dec[KNET_IOBUFFER_SIZE];
	const char *data = kc_lua_get_string(lua, 1);
	
	memset(dec, 0, sizeof(dec));
	
	if (data == NULL) {
		kc_lua_push_nil(lua);
	}
	else {
		if (strlen(data) > KNET_IOBUFFER_SIZE) {
			kc_lua_push_nil(lua);
		}
		else {
			kc_base64_decode(dec, data);
			kc_lua_push_string(lua, dec);
		}
	}
	
	return 1;
}

int kcc_dump_store_file(lua_State *lua)
{
	kc_dump_stores();
	return 0;
}

int kcc_get_random(lua_State *lua)
{
	tms_pool *pool;
	char random[901];
	char *rbuf;
	char enc[KNET_IOBUFFER_SIZE];

	memset(enc, 0, sizeof(enc));

	pool = tms_mem_pool();
	if (pool == NULL) {
		kc_lua_push_string(lua, "nomemory");
		return 1;
	}

	kc_random_buf(random, sizeof(random));
	kc_base64_encode(enc, random, sizeof(random));

	// printf("%s\r\n", enc);

	rbuf = kc_url_encode2(enc, strlen(enc), pool);
	if (rbuf == NULL) {
		kc_lua_push_string(lua, "nomemory");
		tms_del_pool(pool);
		return 1;
	}

	kc_lua_push_string(lua, rbuf);
	tms_del_pool(pool);

	return 1;
}

int kcc_connect(lua_State *lua)
{
	char szhandler[256];
	PRFileDesc *pfd;
	int rc;
	int port;
	knet *pnet;
	const char *ip = kc_lua_get_string(lua, 1);
	
	rc = kc_lua_get_int(lua, 2, &port);
	if (ip == NULL || rc) {
		printf("kcc_connect: get ip and port error\r\n");
		kc_lua_push_nil(lua);
		return 1;
	}

	pfd = knet_connect(ip, port, PR_SecondsToInterval(3));
	if (pfd == NULL) {
		printf("knet_connect: %s - %d error\r\n", ip, port);
		kc_lua_push_nil(lua);
		return 1;
	}
	pnet = knet_new(pfd);
	if (pnet == NULL) {
		printf("knet_connect: %s - %d error\r\n", ip, port);
		PR_Close(pfd);
		kc_lua_push_nil(lua);
		return 1;
	}

	memset(szhandler, 0, sizeof(szhandler));
	kc_base64_encode(szhandler, (const char*)&pnet, sizeof(pnet));

	// printf("knet_connect: %s - %d ok, handler: %s\r\n", ip, port, szhandler);

	kc_lua_push_string(lua, szhandler);
	
	return 1;
}

int kcc_close(lua_State *lua)
{
	knet *pnet;

	const char *handler = kc_lua_get_string(lua, 1);
	if (handler == NULL) {
		printf("kcc_close: no handler\r\n");
		return 0;
	}

	kc_base64_decode(&pnet, handler);
	knet_close(pnet);

	// printf("kcc_close: closed\r\n\r\n");

	return 0;
}

int kcc_send(lua_State *lua)
{
	int rc;
	knet *pnet;
	const char *handler = kc_lua_get_string(lua, 1);
	const char *data = kc_lua_get_string(lua, 2);

	if (handler == NULL || data == NULL) {
		printf("kcc_close: no handler data\r\n");
		return 0;
	}
	
	kc_base64_decode(&pnet, handler);
	
	rc  = knet_write_bin(pnet, data, strlen(data), PR_SecondsToInterval(10));
	rc |= knet_flush(pnet, PR_SecondsToInterval(10));
	if (rc) {
		printf("kcc_send error: %s\r\n", data);
	}
	
	//printf("kcc_send: %s\r\n", data);
	
	return 0;
}

int kcc_sleep(lua_State *lua)
{
	int sp;
	int rc = kc_lua_get_int(lua, 1, &sp);
	
	if (rc) {
		return 0;
	}
	PR_Sleep(sp);
	
	return 0;
}

int main(int argc, char **argv)
{
	char szfile[1024];
	lua_State *lua;
	int rc;

	if (argc < 2) {
		printf("Usage: test_press test.lua\r\n\r\n");
		thomas_get_full_path_a(szfile, sizeof(szfile), NULL, NULL, "test.lua");
	}
	else {
		strcpy(szfile, argv[1]);
	}

	lua = kc_lua_new();
	if (lua == NULL) {
		printf("new lua failed\r\n");
		goto EXIT_TEST;
	}

	// to register test functions
	kc_lua_reg_function(lua, kcc_base64_enc, "kcc_base64_enc");
	kc_lua_reg_function(lua, kcc_base64_dec, "kcc_base64_dec");
	kc_lua_reg_function(lua, kcc_get_random, "kcc_get_random");
	kc_lua_reg_function(lua, kcc_connect, "kcc_connect");
	kc_lua_reg_function(lua, kcc_send, "kcc_send");
	kc_lua_reg_function(lua, kcc_close, "kcc_close");
	kc_lua_reg_function(lua, kcc_sleep, "kcc_sleep");
	kc_lua_reg_function(lua, kcc_dump_store_file, "kcc_dump_store_file");

	rc = kc_lua_run_file(lua, szfile);
	if (rc) {
		kc_lua_close(lua);
		printf("run lua file: %s failed\r\n", szfile);
		goto EXIT_TEST;
	}
	kc_lua_close(lua);

	printf("test done\r\n");

EXIT_TEST:
	getchar();
    return 0;
}
